package cn.lwalan.commonUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DateUtil {

	private static Logger log=LoggerFactory.getLogger(DateUtil.class);
	
	/**
	 * format yyyy-MM-dd HH:mm:ss
	 */
	public static final String FORMAT_YY_M_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	/**
	 * format yyyyMMddHHmmss
	 */
	public static final String FORMAT_YYMDDHHMMSS = "yyyyMMddHHmmss";
	/**
	 * format yyyy-MM-dd
	 */
	public static final String FORMAT_YY_M_DD = "yyyy-MM-dd";
	/**
	 * format yyyyMMdd
	 */
	public static final String FORMAT_YYMDD = "yyyyMMdd";
	
	/**
	 * 转换为时间（天,时:分:秒.毫秒）
	 * @param timeMillis
	 * @return
	 */
    public static String formatDateTime(long timeMillis){
		long day = timeMillis/(24*60*60*1000);
		long hour = (timeMillis/(60*60*1000)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }
	
	/**
	 * 格式化时间
	 * @param date
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return 格式化后的日期
	 */
	public static String formatDate(Date date , String formater){
		if(null == date){
			date = new Date();
		}
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
		String formdate = sdf.format(date);
		return formdate;
	}
	
	/**
	 * Str时间转化为str时间
	 * @param strDate 时间字符串
	 * @param fromFormat 传进来的时间字符串的格式
	 * @param toFromat 要返回的时间字符格式   yyyy-MM-dd HH:mm:ss 为空是默认的
	 * @return
	 */
	public static String strDateFormatToFormat(String strDate,String fromFormat,String toFromat){
		if(StringUtils.isEmpty(toFromat)){
			toFromat = "yyyy-MM-dd HH:mm:ss";
		}
		String tt = null;
		SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
		try {
			Date date = sdf.parse(strDate);
			//SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf2 = new SimpleDateFormat(toFromat);
			tt = sdf2.format(date);
		} catch (ParseException e) {
			log.error("时间转换异常{}",e);
			return null;
		}
		return tt;
	}
	
	/**
	 * 获取xx月前的日期
	 * @param interval
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return 格式化后的日期
	 */
	public static String getBeforeMonth(int interval,String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Calendar c = Calendar.getInstance();  
	    c.add(Calendar.MONTH, - interval);  
	    Date monday = c.getTime();
	    String preMonday = sdf.format(monday);
	    return preMonday;
	}
	
	/**
	 * 获取xx月后的日期
	 * @param strdate yyyy-MM-dd HH:mm:ss 默认的
	 * @param interval
	 * @return 格式化后的日期
	 */
	public static String getAfterMonth(int interval,String strdate,String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Date date = null;
		try {
			date = sdf.parse(strdate);
		} catch (ParseException e) {
			date = new Date();
		}
	    Calendar c = Calendar.getInstance();  
	    c.setTime(date);
	    c.add(Calendar.MONTH, interval);  
	    Date monday = c.getTime();
	    String preMonday = sdf.format(monday);
	    return preMonday;
	}
	
	/**
	 * 获取xx天前的日期
	 * @param interval
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return 格式化后的日期
	 */
	public static String getBeforeDate(int interval,String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Calendar c = Calendar.getInstance();  
	    c.add(Calendar.DATE, - interval);  
	    Date monday = c.getTime();
	    String preMonday = sdf.format(monday);
	    return preMonday;
	}
	
	/**
	 * 获取xx天后的日期
	 * @param strdate yyyy-MM-dd HH:mm:ss 默认的
	 * @param interval
	 * @return 格式化后的日期
	 */
	public static String getAfterDate(int interval,String strdate,String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Date date = null;
		try {
			date = sdf.parse(strdate);
		} catch (ParseException e) {
			date = new Date();
		}
	    Calendar c = Calendar.getInstance();  
	    c.setTime(date);
	    c.add(Calendar.DATE, interval);  
	    Date monday = c.getTime();
	    String preMonday = sdf.format(monday);
	    return preMonday;
	}
	
	/**
	 * 将日期按格式转换
	 * @param date
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return
	 */
	public static String getFormatDate(Date date,String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater)	;
		return sdf.format(date);
	}
	
	/**
	 * 将当前日期按格式转换
	 * 
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return 格式化后的日期
	 */
	public static String getFormatNowDate(String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater)	;
		return sdf.format(new Date());
	}
	
	/**
	 * 根获得星期 
	 * @param strdate yyyy-MM-dd HH:mm:ss
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return 星期几 (0-6)
	 */
	public static String getWeekOfDate(String strdate,String formater) {
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Date date = null;
		try {
			date = sdf.parse(strdate);
		} catch (ParseException e) {
			date = new Date();
		}
		return getWeekOfDate(date);
	}
	/**
	 * 根获得星期 
	 * @param strdate yyyy-MM-dd HH:mm:ss
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return 星期一或星期二
	 */
	public static String getWeekOfDate2(String strdate,String formater) {
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Date date = null;
		try {
			date = sdf.parse(strdate);
		} catch (ParseException e) {
			date = new Date();
		}
		return getWeekOfDate2(date);
	}
	
	/** 
   * 根据日期获得星期
   * @param date
   * @return  星期几(0-6)
   */
	public static String getWeekOfDate(Date date) { 
		//String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
		String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" }; 
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date); 
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; 
		return weekDaysCode[intWeek]; 
	}

	/** 
     * 根据日期获得星期 
     * @param date 
     * @return  星期一或星期二
     */ 
	public static String getWeekOfDate2(Date date) { 
		String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
		//String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" }; 
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date); 
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; 
		return weekDaysName[intWeek]; 
	}

	/** 
	  * 获得周一的日期 
	  * 
	  * @param date 
	  * @return 
	  */ 
	public static String getMonday(Date date,String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		//return dateFormat.format(calendar.getTime());
		return formatDate(calendar.getTime(),format);
	}

	/** 
	  * 获得周三的日期 
	  * 
	  * @param date 
	  * @return 
	  */ 
	public static String getWednesday(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		//return dateFormat.format(calendar.getTime());
		return formatDate(calendar.getTime(),"");
	} 
	/** 
	  * 获得周五的日期 
	  * 
	  * @param date 
	  * @return 
	  */ 
	public static String getFriday(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		//return dateFormat.format(calendar.getTime());
		return formatDate(calendar.getTime(),"");
	}

	/** 
	  * 当前日期前几天或者后几天的日期 
	  * @param n 
	  * @return 
	  */
	public static String afterNDay(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, n);
		Date date = calendar.getTime();
		//String s = dateFormat.format(date);
		String s = formatDate(calendar.getTime(),"");
		return s;
	}

	/** 
	  * 判断两个日期是否在同一周 
	  * 
	  * @param date1 
	  * @param date2 
	  * @return 
	  */ 
	public static boolean isSameWeekDates(Date date1, Date date2){
		Calendar cal1 = Calendar.getInstance(); 
		Calendar cal2 = Calendar.getInstance(); 
		cal1.setTime(date1); 
		cal2.setTime(date2); 
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR); 
		if (0 == subYear){
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2 
					.get(Calendar.WEEK_OF_YEAR)) 
				return true; 
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)){
			// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周 
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2 
					.get(Calendar.WEEK_OF_YEAR)) 
				return true; 
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)){ 
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2 
					.get(Calendar.WEEK_OF_YEAR)) 
				return true; 
		}
		return false; 
	}
	
	/**
	 * 获取当前xx小时前的时间
	 * @param hours
	 * @param formater yyyy-MM-dd HH:mm:ss 默认的
	 * @return
	 */
	public static String getBeforeDateByHH(int hours,String formater){
		if(StringUtils.isEmpty(formater)){
			formater = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
	    Calendar c = Calendar.getInstance();  
	    c.add(Calendar.HOUR, -hours);  
	    Date monday = c.getTime();
	    String preMonday = sdf.format(monday);
	    return preMonday;
	}
	
	/**
	 * 获取时间所在周的结束日期
	 * @param date yyyyMMdd
	 * @return yyyyMMdd
	 */
    public static String getLastDayOfWeek(String date) {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			Date dat = sdf.parse(date);
			Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.setTime(dat);
            calendar.set(Calendar.DAY_OF_WEEK,
                         calendar.getFirstDayOfWeek()+6 );
            
            return sdf.format(calendar.getTime());
		} catch (ParseException e) {
			return null;
		}
    }
    
    /**
     * 获取日期所在周的星期一日期
     * 
     * @param date
     * @param format
     * @return
     */
    public static String getFirstDayOfWeek(String date,String format){
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	Date dat;
		try {
			dat = sdf.parse(date);
		} catch (ParseException e) {
			log.error("获取当前时间所在周的日期异常"+e);
			return null;
		}
    	Calendar calendar = new GregorianCalendar();
    	calendar.setFirstDayOfWeek(Calendar.MONDAY);
    	calendar.setTime(dat);
    	calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        return sdf.format(calendar.getTime());
    }

	/**
   * 获取指定日期格式的前几天或者后几天的日期 ,
   * @param n
   * @return
   */
	public static String afterNDay(int n,String day,String formater) throws ParseException {
		DateFormat fmt =new SimpleDateFormat("yyyyMMdd");
		Date dateDay = fmt.parse(day);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateDay);
		calendar.add(Calendar.DATE, n);
		Date date = calendar.getTime();
		//String s = dateFormat.format(date);
		String s = formatDate(calendar.getTime(),formater);
		return s;
	}

	/**
	 * 当前日期前几天或者后几天的日期 （按指定个格式）
	 * @param n
	 * @param formater
	 * @return
	 */
	public static String afterNDay(int n,String formater) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, n);
		Date date = calendar.getTime();
		//String s = dateFormat.format(date);
		String s = formatDate(calendar.getTime(),formater);
		return s;
	}

	/**
	 * 获取时间所在月的第一天
	 * @param date yyyyMMdd
	 * @return yyyyMMdd
	 */
	public static String getFisrtDayOfMonth(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			Date dat = sdf.parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dat);
			calendar.set(Calendar.DATE, 1);
			return sdf.format(calendar.getTime());
		} catch (ParseException e) {
			return null;
		}
	}
	/**
	 * 获取时间所在月的最后一天
	 * @param date yyyyMMdd
	 * @return yyyyMMdd
	 */
	public static String getLastDayOfMonth(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			Date dat = sdf.parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dat);
			calendar.set(Calendar.DATE, 1);
			calendar.add(Calendar.MONTH, 1);
			calendar.add(Calendar.DATE, -1);
			return sdf.format(calendar.getTime());
		} catch (ParseException e) {
			return null;
		}
	}

	public static void main(String[] args){
		System.out.println(getBeforeMonth(1, "yyyyMM"));
	}
	
}
